<?php
namespace app\member\controller;
use app\common\controller\Base;
class Common extends Base{
	function __construct(){
		parent::__construct();
    	$user=session(config('keys.member_user'));
    	if(empty($user)){
    		$this->redirect(url('login/login'));
    	}
    	$this->user=$user;
		$this->assign('user',$user);
		$this->displayNavs($user['id']); //分配菜单
		$this->checkUserHasAuth($user['id']); //权限验证
		$this->assign('site_name',config('setting.member_title'));

		// 聊天组件 用户信息
		$wsImData=model('member')->wsImData($user['id']);
		$this->assign('wsImData',$wsImData->toJson());
		// 聊天组件 用户未读信息
		$unreadIds=model('weixinlogs')->unreadIds($this->user['id']);
		$this->assign('unreadIds',json_encode($unreadIds));
    }
	function del($id=0){
		if(request()->isAjax()||request()->isPost()){
			$ids=input('ids/a',[]); // 多项删除
	        if($id<=0&&empty($ids)){
	            $this->error(lang('Data does not exist'));
	        }
			$member_id=$this->user['id'];
			$where=['id'=>$id,'member_id'=>$member_id];
			if(!empty($ids)){
				$where=[
					'member_id'=>$member_id,
					'id'=>['id','in',$ids]
				];
			}
			
	        $del=model($this->table)->del($where);
	        if($del){
	            $this->success(lang('Successful operation'));
	            die;
	        }
	        $this->error(lang('Failed operation'));
	    }
    }
	// 上传头像
	function hloads(){
		$file=request()->file('file');
		$user = $this->user;
		$uid = $user['id'];
		if($file){
			$rs=$this->huploads($file,$validate=null,$tb=null,$uid);
			if($rs != ''){
				// $rs['src']=images($rs['link']);
				$file=env('root_path').'public/uploads/'.$user['head_img'];
		        if(is_file($file)){
		            unlink($file);
		        }
		        if(is_array($rs)){
		        	session(config('keys.member_user'),array_merge($user,$rs));
		        }
				return json(lang('Upload success'),0,$rs);
			}
			return json(lang('Upload failure'));
		}
	}
	// 删除上传的文件
	function delImage($id=0,$where=[]){
		if($id>0||is_array($id)||count($where)){
			if(empty($where)){
				if(is_array($id)){
					$where=['id'=>['id','in',$id]];
				}else{
					$where=['id'=>$id];
				}
			}
			$has=model('media')->readAll($where,0,['field'=>'id,link']);
			if(empty($has)){
				return json(lang('Data does not exist'),1);
			}
			$del=model('media')->del($where);
			if(!$del){
				return json(lang('Failed operation'),1);
			}
			foreach ($has as $v) {
				$file=env('root_path').'public/uploads/'.$v['link'];
		        if(is_file($file)){
		            unlink($file);
		        }
			}
			return json(lang('Successful operation'));
		}
	}
	// 删除上传的文件
	function delImages($id=0,$where=[]){
		if($id>0||is_array($id)||count($where)){
			if(empty($where)){
				if(is_array($id)){
					$where=['id'=>['id','in',$id]];
				}else{
					$where=['id'=>$id];
				}
			}
			$has=model('media')->readAll($where,0,['field'=>'id,link']);
			if(empty($has)){
				$this->error(lang('Data does not exist'));
			}
			$del=model('media')->del($where);
			if(!$del){
				$this->error(lang('Failed operation'));
			}
			foreach ($has as $v) {
				$file=env('root_path').'public/uploads/'.$v['link'];
		        if(is_file($file)){
		            unlink($file);
		        }
			}
			$this->success(lang('Successful operation'));
		}
	}
	function getDevices(){
		if(request()->isAjax()||request()->isPost()){
			$ids=input('ids/a',[]);
			if(empty($ids)){
				$this->error('数据不存在');
			}
			$where=[
				'member_id'=>$this->user['id'],
				'id'=>['id','in',$ids]
			];
			$list=model('device')->readAll($where);
			if(count($list)){
				$this->success('获取成功','',$list->toJson());
			}
		}
	}
}
